package edu.bw.mapper;

import edu.bw.po.User;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserMapper {

    /**
     * 根据用户的id查询用户信息
     *
     * @param id 用户id
     * @return 用户信息
     */
    @Select("SELECT id,username,birthday,gender,address FROM mybatis02.`user` WHERE id=#{id}")
    User findById(int id);

    /**
     * 查询所有用户(延迟加载 用户关联的订单 订单关联的订单明细 订单明细关联的商品)
     *
     * @return
     */

    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(property = "orders", javaType = List.class, column = "id",
                    many = @Many(select = "edu.bw.mapper.OrderMapper.findOrdersByUserId", fetchType = FetchType.LAZY))
    })
    @Select("SELECT id,username,birthday,gender,address FROM mybatis02.`user`")
    List<User> findAllLazy();
}
